<template>
	<view>
		<!-- 更换造价员 -->
		<changeManCom :show="upManVisible" :projectId="oper_item.id" :type="3" @closeFunc="close('upMan')"
			@changeManSuccessFunc="changeManSuccessFunc"></changeManCom>
		<!-- 分配造价员 -->
		<u-picker title="分配造价员" :show="fpVisible" :columns="adminList" keyName="name" closeOnClickOverlay
			@close="close('fp')" @cancel="close('fp')" @confirm="fpSubmit" confirmColor="#3B75FF"></u-picker>
		<!-- 审核 -->
		<u-popup :show="shVisible" @close="close('sh')" round="10" closeable>
			<view class="popup-title" v-if="oper_item.status == 1090">报价单初审</view>
			<view class="popup-title" v-if="oper_item.status == 1092">报价单复审</view>
			<view class="popup-box">
				<u-radio-group v-model="formData.reviewPassed" placement="row" activeColor="#3B75FF">
					<u-radio :name="1" label="通过" :customStyle="{marginRight: '40rpx'}"></u-radio>
					<u-radio :name="2" label="拒绝"></u-radio>
				</u-radio-group>
				<view class="textarea-box">
					<u-textarea v-model="formData.remark" placeholder="请输入审批意见"></u-textarea>
				</view>
				<u-button type="primary" text="提交" color="#3B75FF" :customStyle="{marginTop: '30rpx'}"
					@click="shSubmit"></u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		name: "budgetManagement",
		props: {
			value: {
				type: String,
				default: ''
			},
			oper_item: {
				type: Object,
				default: {}
			}
		},
		data() {
			return {
				// 更换造价员
				upManVisible: false,
				// 分配造价员
				fpVisible: false,
				adminList: [],
				// 审核
				shVisible: false,
				formData: {
					reviewPassed: 1,
					remark: '',
				},
			};
		},
		watch: {
			value(val) {
				console.log(val, 'xxxx');
				switch (val) {
					// 更换造价员
					case 'preSettlement_editZjy':
						this.upManVisible = true;
						break;
						// 分配造价员
					case 'preSettlement_addZjy':
						if (this.adminList.length == 0) {
							this.getAdminList()
						} else {
							this.fpVisible = true;
						}
						break;
						break;
					case 'preSettlement_sh':
						this.shVisible = true;
						break;
				}
			}
		},
		methods: {
			close(type) {
				switch (type) {
					case 'upMan':
						this.upManVisible = false;
						break;
					case 'fp':
						this.fpVisible = false;
						break;
					case 'sh':
						this.shVisible = false;
						this.formData = {
							reviewPassed: 1,
							remark: '',
						}
						break;
				}
				this.$emit('update:value', '');
			},
			/********** 更换造价员 **********/
			changeManSuccessFunc() {
				this.close('upMan')
				this.$emit('submit', this.value);
			},
			/********** 分配造价员 **********/
			// 获取负责人列表
			getAdminList() {
				this.$Apipc.getAdminList({
					page: 1,
					num: 99,
					identity: 1,
					position_id_arr: [5],
				}).then(res => {
					console.log(res, '获取负责人');
					if (res.code == 200) {
						this.adminList[0] = res.data.list;
						this.fpVisible = true;
					}
				})
			},
			fpSubmit(e) {
				uni.showLoading({
					mask: true
				});

				let oper_item = this.oper_item
				this.$hApi.assignCostEstimator({
					project_id: oper_item.id,
					cost_estimator_id: e.value[0].id,
					remark: '',
				}).then(res => {
					console.log(res, '分配造价员');
					uni.hideLoading();
					if (res.code == 200) {
						this.$emit('submit', this.value);
						this.close('fp')
					}
					uni.showToast({
						title: res.code == 200 ? "操作成功" : res.message,
						icon: res.code == 200 ? "success" : "error",
					});
				})
			},
			/********** 审核 **********/
			shSubmit() {
				let oper_item = this.oper_item
				let formData = this.formData
				if (formData.reviewPassed == 1) {
					uni.showLoading({
						mask: true
					});
					let audit_type = 1;
					if (oper_item.status == 1090) audit_type = 1;
					if (oper_item.status == 1092) audit_type = 2;
					this.$hApi.quotationReviewPassed({
						quotation_id: "",
						project_id: oper_item.id,
						remark: formData.remark,
						audit_type // this.userInfo.position_id == 7 ? 1 : 2
					}).then(res => {
						console.log(res, 'quotationReviewPassed');
						uni.hideLoading();
						if (res.code == 200) {
							this.$emit('submit', this.value);
							this.close('sh')
						}
						uni.showToast({
							title: res.code == 200 ? "操作成功" : res.message,
							icon: res.code == 200 ? "success" : "error",
						});
					})
				} else {
					if (!formData.remark) {
						this.$refs.uToast.show({
							message: "请输入审批意见",
						})
						return
					}
					uni.showLoading({
						mask: true
					});
					this.$hApi.quotationReviewRejected({
						quotation_id: "",
						project_id: oper_item.id,
						remark: formData.remark,
					}).then(res => {
						console.log(res, 'quotationReviewRejected');
						uni.hideLoading();
						if (res.code == 200) {
							this.$emit('submit', this.value);
							this.close('sh')
						}
						uni.showToast({
							title: res.code == 200 ? "操作成功" : res.message,
							icon: res.code == 200 ? "success" : "error",
						});
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.popup-title {
		height: 46px;
		border-bottom: 2rpx solid #efefef;
		text-align: center;
		line-height: 46px;
		font-size: 30rpx;
	}

	.popup-box {
		padding: 30rpx 20rpx 20rpx;

		.textarea-box {
			margin-top: 30rpx;
		}
	}
</style>